package com.meirengu.webview.service.impl;

import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.HttpUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.dao.KwZhudaiPushDao;
import com.meirengu.webview.model.KwZhudaiPush;
import com.meirengu.webview.model.KwZhudaiPushLog;
import com.meirengu.webview.service.KwAreaDataService;
import com.meirengu.webview.service.KwZhudaiPushLogService;
import com.meirengu.webview.service.KwZhudaiPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * KwZhudaiPush服务实现层
 *
 * @author Mao
 * @create Wed May 09 11:07:00 CST 2018
 */
@Service
public class KwZhudaiPushServiceImpl extends BaseServiceImpl<KwZhudaiPush> implements KwZhudaiPushService {


    Logger logger = LoggerFactory.getLogger(KwZhudaiPushServiceImpl.class);

    @Autowired
    private KwZhudaiPushLogService kwZhudaiPushLogService;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private KwAreaDataService kwAreaDataService;
    @Autowired
    private KwZhudaiPushDao kwZhudaiPushDao;

    /**
     * @param
     * @return
     */
    @Override
    public Integer applyLoan(String name, String mobile, String city, String car,
                             int age, String job, String house, String baodanIs,
                             String sex, int money, String shebao, String gongjijin,
                             String isbankpay, String ip, String creditCard,
                             String meiti, String time, int weili, String checkCode) throws Exception {

        String url = ConfigUtil.getConfig("zhudai.url");
        String source = ConfigUtil.getConfig("zhudai.source");

        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("mobile", mobile);
        params.put("city", city);
        params.put("car", car);
        params.put("age", age + "");
        params.put("job", job);
        params.put("house", house);
        params.put("baodan_is", baodanIs);
        params.put("sex", sex);
        params.put("money", money + "");
        params.put("source", source);
        params.put("shebao", shebao);
        params.put("gongjijin", gongjijin);
        params.put("isbankpay", isbankpay);
        params.put("ip", ip);
        params.put("credit_card", creditCard);
        params.put("meiti", meiti);
        /*时间改为实时，通过控制单位时间内的传送数量来达到自然用户注册的假象*/
        params.put("time", time.substring(0, 10));
        params.put("weili", weili + "");
        params.put("check_num", checkCode);
        /*long registerTime = System.currentTimeMillis()-5000;
        logger.info("用户注册时间：{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(registerTime)));
        String timeStr = registerTime + "";
        params.put("time", timeStr.substring(0,10));
*/

        //接口调用信息表
        KwZhudaiPush kwZhudaiPush = new KwZhudaiPush();
        kwZhudaiPush.setInterfaceCode("zhudai");
        kwZhudaiPush.setInterfaceName("助贷网");
        kwZhudaiPush.setMobile(mobile);
        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwZhudaiPushLog kwZhudaiPushLog = new KwZhudaiPushLog();
        kwZhudaiPushLog.setInterfaceCode("zhudai");
        kwZhudaiPushLog.setInterfaceName("助贷网");
        kwZhudaiPushLog.setMobile(mobile);
        kwZhudaiPushLog.setSendUrl(url.toString());
        kwZhudaiPushLog.setParams(params.toString());

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                kwZhudaiPushLog.setBackUrl(hr.toString());
                kwZhudaiPushLog.setHttpCode(200);
                kwZhudaiPushLog.setHttpContent(hr.toString());
                logger.info("==ZhuDaiServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                if ("n".equals(content)) {
                    kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwZhudaiPush.setMessage("不再接收数据");
                    logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                } else {
                    Integer rCode = 0;
                    rCode = Integer.parseInt(content);
                    if (rCode.intValue() > 1000000) {
                        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                        kwZhudaiPush.setMessage("成功");
                        logger.info("==ZhuDaiServiceImpl.applyLoan business success");
                    } else if (rCode.intValue() == 3) {
                        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                        kwZhudaiPush.setMessage("指定时间内重复申请");
                        logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                    } else if (rCode.intValue() == 5) {
                        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_FAIL);
                        kwZhudaiPush.setMessage("失败");
                        logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                    } else if (rCode.intValue() == 6) {
                        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_IP);
                        kwZhudaiPush.setMessage("恶意IP");
                        logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                    } else if (rCode.intValue() == 7) {
                        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_MALICIOUS_PHONE);
                        kwZhudaiPush.setMessage("恶意电话");
                        logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                    } else {
                        kwZhudaiPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                        kwZhudaiPush.setMessage("rCode: " + rCode);
                        logger.warn("==ZhuDaiServiceImpl.applyLoan business fail");
                    }
                }
            } else {
                kwZhudaiPushLog.setBackUrl(hr.toString());
                kwZhudaiPushLog.setHttpCode(code);
                kwZhudaiPushLog.setHttpContent(hr.toString());
                logger.warn("==ZhuDaiServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwZhudaiPushLog.setHttpCode(408);
            kwZhudaiPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            insert(kwZhudaiPush);
            kwZhudaiPushLog.setPushId(kwZhudaiPush.getId());
            kwZhudaiPushLogService.insert(kwZhudaiPushLog);
            return kwZhudaiPush.getCode();
        }
    }

    @Override
    public List<KwZhudaiPush> getModel(Map map) {
        return kwZhudaiPushDao.getModel(map);
    }


}
